package com.luzhikun.myshopping.dao.impl;

import com.luzhikun.myshopping.dao.ProductDao;
import com.luzhikun.myshopping.domaim.Product;
import com.luzhikun.myshopping.util.DBCPUtil;

import java.util.List;

/**
 * @Author Rookie Coder
 * @Description
 * @Date
 **/
public class ProductDaoImpl implements ProductDao {

    /*
     * @Author liu-miss
     * @Description 查询所有商品信息
     * @Date 19:44 2021/7/12
     * @Param []
     * @return java.util.List<com.luzhikun.myshopping.domaim.Product>
     **/

    @Override
    public List<Product> selectAllProduct(int pageNow,int pageCount) {
        DBCPUtil dbcpUtil=new DBCPUtil();
        String sql = "select * from product limit ?,?";
        List<Product> query = dbcpUtil.query(sql, Product.class, pageNow,pageCount);
        return query;
    }

    /*
     * @Author liu-miss
     * @Description 删除指定商品
     * @Date 20:31 2021/7/12
     * @Param [pid]
     * @return boolean
     **/
    @Override
    public boolean delectProductById(String pid) {
        DBCPUtil dbcpUtil=new DBCPUtil();
        String sql = "delete from product where pid = ?";
        int update = dbcpUtil.update(sql, pid);
        return update>0;
    }

    /*
     * @Author liu-miss
     * @Description添加商品信息
     * @Date 21:53 2021/7/12
     * @Param [product]
     * @return boolean
     **/
    @Override
    public boolean addProduct(Product product) {
        DBCPUtil dbcpUtil=new DBCPUtil();
        String sql ="insert into product values(?,?,?,?)";
        int update = dbcpUtil.update(sql, product.getPid(), product.getPname(), product.getPdate(), product.getPfactory());
        return update>0;
    }

    /*
     * @Author liu-miss
     * @Description 查询一个商品
     * @Date 10:32 2021/7/16
     * @Param [pid]
     * @return void
     **/
    @Override
    public Product selectOneProduct(String pid) {
        String sql = "select * from product where pid = ?";
        DBCPUtil dbcpUtil= new DBCPUtil();
        List<Product> query = dbcpUtil.query(sql, Product.class, pid);
        System.out.println(query);
        if (query!=null){
            System.out.println(query.get(0));
            return query.get(0);
        }
        return null;
    }

    @Override
    public int selectCount() {
        String sql="select count(pid) from product";
        DBCPUtil dbcpUtil =new DBCPUtil();
        Long query = (Long) dbcpUtil.query(sql, null);
        if (query!=null){
            System.out.println(query.getClass());
            return query.intValue();
        }
        return 0;
    }

}
